package com.tzbank.common.service.impl;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.tzbank.common.dao.PushReqMsgMapper;
import com.tzbank.common.service.PushReqMsgService;
import com.tzbank.entity.mpass.PushReqMsg;
import com.tzbank.utils.String2Utils;

import lombok.extern.slf4j.Slf4j;

/**
 * @author tzbank
 *
 */
@Service
@Slf4j
public class PushReqMsgServiceImpl implements PushReqMsgService {

	@Resource
	private PushReqMsgMapper pushReqMsgMapper;

	@Transactional
	@Override
	public boolean add(Map<String,Object> reqBody) {
		log.info("保存推送消息 请求参数:{}",JSON.toJSONString(reqBody));
		String taskName = reqBody.get("taskName").toString();
		String title = reqBody.get("title").toString();
		String content = reqBody.get("content").toString();
		String uri = reqBody.get("uri").toString();
		String pushState = reqBody.get("pushState").toString();
		String readStatus = reqBody.get("readStatus").toString();
		String remark = reqBody.get("remark").toString();
		
		//消息模板表记录id
		String templateId = reqBody.get("templateId").toString();
		
		
		PushReqMsg msg = new PushReqMsg();
		msg.setMsgId("MSG"+String2Utils.getGenerateUUID());
		msg.setTemplateId(templateId);
		
		msg.setTaskName(taskName);
		msg.setTitle(title);
		msg.setContent(content);
		msg.setUri(uri);
		//msg.setDeliveryType(Integer.valueOf(deliveryType));
		//msg.setExpiredSeconds(expiredSeconds);
		//msg.setTargetMsgkey(targetMsgkey);
		msg.setPushState(pushState);
		//msg.setMsgType(msgType);
		//msg.setShowMode(showMode);
		msg.setReadStatus(readStatus);
		//msg.setPushTag(pushTag);
		//msg.setPushObject(pushObject);
		msg.setRemark(remark);
		
		//Date date =  DateUtils.getConversionDateByLong(Long.parseLong(pushDate));
		//msg.setPushDate(date);
		
		return pushReqMsgMapper.insertSelective(msg) > 0;
	}
	
	@Transactional
	@Override
	public boolean updateReadStatusByMsgId(Map<String,Object> reqBody) {
		PushReqMsg msg = new PushReqMsg();
		String msgId = reqBody.get("msgId").toString();
		msg.setMsgId(msgId);
		msg.setReadStatus("1");
		return pushReqMsgMapper.updateByMsgSelective(msg) > 0;
	}

	
	public static void main(String[] args) {
/*		System.err.println("11");

		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("date", new Date());
		Date d = (Date) map.get("date");
		System.err.println(d);*/
		
		
		//DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		
	//String da=	"2018-06-08 12:22:22";
		
	//LocalDateTime time  = LocalDateTime.parse(string,formatter);
	
	//System.err.println(time);
	//	//System.err.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
		/*DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("");
		LocalDate.parse("",dateTimeFormatter);*/
		/*String sd = df.format(time);
		Date xa = LocalDateTime.parse(sd, df);
		System.out.println(string);*/
		
		
		ZoneId zid = ZoneId.systemDefault();
		System.out.println(zid);
		
		String pushDate = "1528701733648";
		
		LocalDateTime time = LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(pushDate)), zid);
		ZonedDateTime zdt = time.atZone(zid);
		Date date = Date.from(zdt.toInstant());
		System.out.println(date);
		
	}
	
	
}
